/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool dfs(TreeNode* root)
    {
        if (!root) return true;
        if (!dfs(root->left))
            root->left = nullptr;
        if (!dfs(root->right))
            root->right = nullptr;
        if (root->val == 0 && !root->left && !root->right)
            return false;
        return true;

    }
    TreeNode* pruneTree(TreeNode* root) {
        bool ret = dfs(root);
        if (root->val == 0 && ret == false)
            root = nullptr;
        return root;
    }
};

class Solution {
public:
    void dfs(vector<int>& a, vector<int>& b, vector<int>& c, int n)
    {
        if (n == 1)
        {
            c.push_back(a.back());
            a.pop_back();
            return;
        }
        dfs(a, c, b, n - 1);
        c.push_back(a.back());
        a.pop_back();
        dfs(b, a, c, n - 1);
    }
    void hanota(vector<int>& A, vector<int>& B, vector<int>& C) {
        dfs(A, B, C, A.size());
    }
};

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    long prev = LONG_MIN;
    bool dfs(TreeNode* root)
    {
        if (!root)return true;
        bool res = dfs(root->left);

        if (!res)
            return res;
        if (prev < root->val)
            prev = root->val;
        else
            return false;
        res = dfs(root->right);

        return res;
    }
    bool isValidBST(TreeNode* root) {

        return dfs(root);
    }
};

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int count = 0;
    int ret = 0;
    int kthSmallest(TreeNode* root, int k) {
        count = k;
        dfs(root);
        return ret;
    }
    void dfs(TreeNode* root)
    {
        if (root == nullptr)return;
        if (count == 0)return;
        dfs(root->left);
        if (count == 1)
            ret = root->val;
        count--;
        dfs(root->right);
    }
};


class Solution {
public:
    vector<vector<int>> ret;
    int memory[7];
    int count;
    vector<vector<int>> permute(vector<int>& nums) {
        count = nums.size();
        vector<int> tmp;
        dfs(nums, tmp);
        return ret;
    }
    void dfs(vector<int>& nums, vector<int>& tmp)
    {
        if (tmp.size() == count)
        {
            ret.push_back(tmp);
        }
        for (int i = 0; i < nums.size(); i++)
        {
            if (memory[i] == 0)
            {
                memory[i] = 1;
                tmp.push_back(nums[i]);
                dfs(nums, tmp);
                tmp.pop_back();
                memory[i] = 0;
            }
        }
    }
};


class Solution {
public:
    vector<vector<int>> ret;
    int memory[11];
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<int> tmp;
        dfs(nums, tmp, 0);
        return ret;
    }
    void dfs(vector<int>& nums, vector<int>& tmp, int n)
    {
        ret.push_back(tmp);

        for (int i = n; i < nums.size(); i++)
        {
            if (memory[i] == 0)
            {
                memory[i] = 1;
                tmp.push_back(nums[i]);
                dfs(nums, tmp, i + 1);
                tmp.pop_back();
                memory[i] = 0;
            }
        }
    }
};


